
const mp = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        let temp = arr[j]
        arr[j] = arr[j + 1]
        arr[j + 1] = temp
      }
    }
  }
  return arr
}
// console.log(mp([5,4,3,2,1,6,9,7,8]));
const xz = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    let minIdx = i
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[minIdx] > arr[j]) {
        minIdx = j
      }
    }
    if (minIdx !== i) {
      let temp = arr[minIdx]
      arr[minIdx] = arr[i]
      arr[i] = temp
    }
  }
  return arr
}
// console.log(xz([5,4,3,2,1,8,6,7]));

const cr = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    let temp = arr[i]
    let j = i
    while (j > 0) {
      if (arr[j - 1] > temp) {
        arr[j] = arr[j - 1]
      } else {
        break
      }
      j--
    }
    arr[j] = temp
  }
  return arr
}
// console.log(cr([5,4,3,2,1,8,6,7]));

const ks = (array) => {
  const rec = (arr) => {
    if (arr.length <= 1) {
      return arr
    }
    const left = []
    const right = []
    const mid = arr[0]

    for (let i = 1; i < arr.length; i++) {
      arr[i] > mid ? right.push(arr[i]) : left.push(arr[i])
    }
    return [...rec(left), mid, ...rec(right)]
  }
  return rec(array);
}
console.log(ks([5,4,3,2,1,8,6,7]));